﻿
Public Class frmProveedor

    Private Sub frmProveedor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CargarProveedor()
    End Sub

    Dim controller As New ControllerSigeco.controller_compras()

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private Shadows Sub KeyUpmask(ByVal MaskedTextboxs As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Tab Or e.KeyData = Keys.Enter _
            Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And MaskedTextboxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            MaskedTextboxs.Text.Remove(MaskedTextboxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf MaskedTextboxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            MaskedTextboxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            MaskedTextboxs.Text = String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            Beep()
            MsgBox("Introducir solo valores numericos")

        End If

    End Sub

    Private Sub txttelefono_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTelefono1.KeyUp
        'Validar que solo se ingresen valores numericos
        Me.KeyUpmask(Me.txtTelefono1, e)

    End Sub

    Private Sub txtcelular_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTelefono2.KeyUp
        'Validar que solo se ingresen valores numericos
        Me.KeyUpmask(Me.txtTelefono2, e)
    End Sub

    Private Shadows Sub KeyUp(ByVal TextBoxs As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Tab Or e.KeyData = Keys.Enter _
           Or e.KeyData = Keys.OemMinus Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And TextBoxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            TextBoxs.Text.Remove(TextBoxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf TextBoxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            TextBoxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            TextBoxs.Text = String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            Beep()
            MsgBox("Introducir solo valores numericos")
        End If

    End Sub

    Private Sub CargarProveedor()
        Try
            If codProveedor > 0 Then

                Dim ds As New DataTable
                ds = controller.get_Proveedores_All(oiml.dbConn, codProveedor, "", "", False)

                If Not ds Is Nothing Then

                    ' cargar los controles del formulario con los valores de los campos del registro
                    codProveedor = ds.Rows(0)("idProveedor").ToString
                    Me.txtProveedor.Text = ds.Rows(0)("proveedor").ToString
                    Me.txtdireccion.Text = ds.Rows(0)("direccion").ToString
                    Me.txtTelefono1.Text = ds.Rows(0)("telefono1").ToString
                    Me.txtTelefono2.Text = ds.Rows(0)("telefono2").ToString
                    Me.txtEmail.Text = ds.Rows(0)("email").ToString
                    Me.txtruc.Text = ds.Rows(0)("ruc").ToString
                    cbxHabilitado.Checked = Boolean.Parse(ds.Rows(0)("habilitado").ToString)
                    txtActualizadoPor.Text = ds.Rows(0)("usuario").ToString
                    txtEnFecha.Text = ds.Rows(0)("actualizadoFecha_").ToString

                End If

            Else

                Me.txtProveedor.Text = ""
                Me.txtdireccion.Text = ""
                Me.txtTelefono1.Text = ""
                Me.txtTelefono2.Text = ""
                Me.txtEmail.Text = ""
                Me.txtruc.Text = ""
                cbxHabilitado.Checked = True
                txtActualizadoPor.Text = oiml.Usuario
                txtEnFecha.Text = Date.Now.ToShortDateString

                txtProveedor.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btneliminar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        Try
            If codProveedor > 0 Then
                If MessageBox.Show("Desea eliminar el registro seleccionado?", "Proveedores", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                    If controller.Delete_Proveedores(oiml.dbConn, codProveedor) Then
                        Me.Close()

                    Else
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End If

                End If
            End If


        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtProveedor.Text = "" Then
            ErrorProvider1.SetError(txtProveedor, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtProveedor, "")
        End If

        If txtdireccion.Text = "" Then
            ErrorProvider1.SetError(txtdireccion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtdireccion, "")
        End If

        If txtruc.Text = "" Then
            ErrorProvider1.SetError(txtruc, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtruc, "")
        End If
        '
        Return controlCampos

    End Function

    Private Sub btnguardar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            txtProveedor.Focus()
            Exit Sub
        End If

        If codProveedor = 0 Then

            codProveedor = controller.Insert_Proveedores(oiml.dbConn, txtProveedor.Text, txtdireccion.Text, txtTelefono1.Text, _
                                                         txtTelefono2.Text, txtruc.Text, txtEmail.Text, oiml.idUsuario)

            If codProveedor > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If controller.Update_Proveedores(oiml.dbConn, codProveedor, txtProveedor.Text, txtdireccion.Text, txtTelefono1.Text, _
                                                         txtTelefono2.Text, txtruc.Text, txtEmail.Text, oiml.idUsuario, cbxHabilitado.Checked) Then

                MessageBox.Show("Registro grabado con exito!", "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub btncancelar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub


    Private Sub btnEstadoCuenta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEstadoCuenta.Click

        If codProveedor > 0 Then
            Dim frmEstadoCuenta As New frmProveedorEstadoCuenta
            frmEstadoCuenta.codProveedor = codProveedor
            frmEstadoCuenta.lbProveedor = txtProveedor.Text
            frmEstadoCuenta.ShowDialog()
        End If

    End Sub

End Class